import java.util.Arrays;

/**
 * 面试题45：把数组排成最小的数
 */
public class Offer_45 {
    /**
     * 精选题解：自定义排序规则
     * <p>
     * 时间复杂度：O(nlogn)
     * <p>
     * 空间复杂度：O(n)
     */
    public String minNumber(int[] nums) {
        int len = nums.length;
        String[] strs = new String[len];
        for (int i = 0; i < len; i++) {
            strs[i] = String.valueOf(nums[i]);
        }

        Arrays.sort(strs, (x, y) -> (x + y).compareTo(y + x));
        StringBuilder builder = new StringBuilder();
        for (String str : strs) {
            builder.append(str);
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        Offer_45 solution = new Offer_45();
        int[] nums = { 3, 30, 34, 5, 9 };
        String ans = solution.minNumber(nums);
        System.out.println(ans);
    }
}
